package com.xinsilu.hospital.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xinsilu.hospital.entity.AuthsysPermission;
import com.xinsilu.hospital.entity.AuthsysResources;
import com.xinsilu.hospital.mapper.AuthsysPermissionMapper;
import com.xinsilu.hospital.mapper.AuthsysResourcesMapper;
import com.xinsilu.hospital.service.IAuthsysResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ym
 * @since 2021-11-30
 */
@Service
public class AuthsysResourcesServiceImpl extends ServiceImpl<AuthsysResourcesMapper, AuthsysResources> implements IAuthsysResourcesService {

    @Autowired
    private AuthsysResourcesMapper resourcesMapper;
    @Autowired
    private AuthsysPermissionMapper permissionMapper;
    @Override
    public List<AuthsysResources> searchAllResources() {
        return this.resourcesMapper.selectAllResources();
    }

    @Override
    public AuthsysResources searchResource(int id) {
        return this.resourcesMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addResource(AuthsysResources resources) {
        this.resourcesMapper.insertResource(resources);
        List<AuthsysPermission> permissionList = resources.getPermissionList();
        if(permissionList != null){
            for(AuthsysPermission permission:permissionList) {
                permission.setRscId(resources.getRscId());
                this.permissionMapper.insert(permission);
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateResource(AuthsysResources resources) {
        this.resourcesMapper.updateByPrimaryKey(resources);
        this.permissionMapper.deleteByRscId(resources.getRscId());
        List<AuthsysPermission> permissionList = resources.getPermissionList();
        if(permissionList != null){
            for(AuthsysPermission permission:permissionList) {
                this.permissionMapper.insert(permission);
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteResource(int id) {
        this.permissionMapper.deleteByRscId(id);
        this.resourcesMapper.deleteByPrimaryKey(id);
    }
}
